package com.pantuo.sys.service.impl;

import com.alibaba.fastjson.JSON;

import com.pantuo.sys.pojo.Log;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 炳秦
 * @Date: 2021/10/12/11:46
 * @Description:
 */
@Service
public class EsService2 {

    @Autowired
    private RestHighLevelClient client;

    public void testInsert2(){
        System.out.println("定时任务开始");
        //112.6.162.57:9023
        String url = "jdbc:mysql://112.6.162.57:9023/oa_data3?serverTimezone=GMT%2B8&characterEncoding=utf8";
        String user = "root";
        String password = "pantuo1709";
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int index = 1;
        //int length = 10000;
        //int currentPage = 1;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, user, password);
            String sql = "select * from tn_sfzlog ";
            //String sql = "select * from tb_item where id <= 500000";
            statement = conn.prepareStatement(sql);
            //关键：做一下设置会采用流数据接收方式，每次从服务器接收部分数据，直到所有数据处理完毕，不会发生JVM OOM
            statement.setFetchSize(Integer.MIN_VALUE);
            resultSet = statement.executeQuery();
            List<Log> logList = new ArrayList<>();
            BulkRequest bulkRequest = new BulkRequest();
            while (resultSet.next()) {
                System.out.println("fdddd");
                int id = resultSet.getInt("id");
                Date created_dt = resultSet.getDate("created_dt");
                Date updated_dt = resultSet.getDate("updated_dt");
                String lineName_s = resultSet.getString("lineName_s");
                String deviceid_s = resultSet.getString("deviceid_s");
                String vehicleNum1_s = resultSet.getString("vehicleNum1_s");
                String vehicleNum2_s = resultSet.getString("vehicleNum2_s");
                String result_s = resultSet.getString("result_s");
                String r_s = resultSet.getString("r_s");
                String age_s = resultSet.getString("age_s");
                String image_s = resultSet.getString("image_s");
                String name_s = resultSet.getString("name_s");
                String time_s = resultSet.getString("time_s");
                String res_s = resultSet.getString("res_s");
                String log_s = resultSet.getString("log_s");
                String devVersion_s = resultSet.getString("devVersion_s");
                Integer bind_i = resultSet.getInt("bind_i");
                String afterUpdateName_s = resultSet.getString("afterUpdateName_s");
                String afterUpdateCardId_s = resultSet.getString("afterUpdateCardId_s");
                String companyName_s = resultSet.getString("companyName_s");
                logList.add(new Log(id,
                        created_dt,
                        updated_dt,
                        lineName_s,
                        deviceid_s,
                        vehicleNum1_s,
                        vehicleNum2_s,
                        result_s,
                        r_s,
                        age_s,
                        image_s,
                        name_s,
                        time_s,
                        res_s,
                        log_s,
                        devVersion_s,
                        bind_i,
                        afterUpdateName_s,
                        afterUpdateCardId_s,
                        null,
                        companyName_s));
            }
            for (int i = 0; i < logList.size(); i++) {
                bulkRequest.add(new IndexRequest("tn_sfzlog")
                        .source(JSON.toJSONString(logList.get(i)), XContentType.JSON));
            }
            TimeValue took = client.bulk(bulkRequest, RequestOptions.DEFAULT).getTook();
            //index = (currentPage * length) + 1;
            //currentPage = currentPage + 1;
            System.out.println("花费时间：" + took);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }


































}
